
var fm = Ext.form;
var base_url = '<?php echo base64_decode($_GET['baseUrl']);?>';
<?php
	$langList = unserialize(base64_decode($_GET['langList']));
	$langFilter = array();
	foreach($langList as $k=>$v){
		$langFilter[]=array($k,$v);
	}
	$langFilter = json_encode($langFilter);
	$langList = json_encode($langList);
?>
var langFilter = <?php echo $langFilter;?>;
var langList = <?php echo $langList;?>;
var total_row = 50;
var proxy = new Ext.data.ScriptTagProxy({
 	api: {
        read : base_url+'admin/onlinesupport/grid.html',
        create : base_url+'admin/onlinesupport/add.html',
        update: base_url+'admin/onlinesupport/add.html',
        destroy: base_url+'admin/onlinesupport/delete.html'
    }
});

var writer = new Ext.data.JsonWriter({
    encode: true
});

var store = new Ext.data.JsonStore({
	start : 'start',
	remoteSort: true,
    totalProperty: 'total',
    proxy: proxy,
    root: 'data',
    fields : [
              {name: 'id'},
{name: 'name'},
{name: 'position'},
{name: 'yahoo'},
{name: 'sky'},
{name: 'msm'},
{name: 'status',type: 'bool'},
{name: 'edit',type: 'string'}
          ],
	  listeners: {
	      save: function(s, b, d) {
	    	  store.save();
	      }
	  },
    writer: writer
});

var filters = new Ext.ux.grid.GridFilters({
    // encode and local configuration options defined previously for easier reuse
    encode: true, // json encode the filter query
    local: true,   // defaults to false (remote filtering)
    filters: [
        {type: 'string',dataIndex: 'name'},
{type: 'string',dataIndex: 'position'},
{type: 'string',dataIndex: 'yahoo'},
{type: 'string',dataIndex: 'sky'},
{type: 'string',dataIndex: 'msm'},
{type: 'list',dataIndex: 'status',options: [[1,'Available'],[2,'Unavailable']],phpMode: true}
    ]
});
var createColModel = function () {

    var columns = [
    {header: 'ID',sort: 'id',dataIndex: 'id'},
{header: 'Name',sortable: true,sort: 'name',dataIndex: 'name',editor: new Ext.form.TextField({})},
{header: 'Position',sortable: true,sort: 'position',dataIndex: 'position',editor: new Ext.form.TextField({})},
{header: 'YAHOO',sortable: true,sort: 'yahoo',dataIndex: 'yahoo',editor: new Ext.form.TextField({})},
{header: 'SKYPE',sortable: true,sort: 'sky',dataIndex: 'sky',editor: new Ext.form.TextField({})},
{header: 'MSM',sortable: true,sort: 'msm',dataIndex: 'msm',editor: new Ext.form.TextField({})},
{header: 'Status',sortable: false,dataIndex: 'status',xtype:'checkcolumn'},
{header: 'Edit',dataIndex: 'edit',sortable: false}
               ];

    return new Ext.grid.ColumnModel({
        columns: columns,
        defaults: {
            sortable: true
        }
    });
};
pagingBar = new Ext.PagingToolbar({
            store: store,
            pageSize: total_row,
            displayInfo: true,
            displayMsg: 'Displaying item {0} - {1} of {2}',
            emptyMsg: "No item to display",
            plugins: new Ext.ux.ProgressBarPager()
        });
Ext.onReady(function() {
    Ext.QuickTips.init();

    var newsGrid = new Ext.grid.EditorGridPanel({
        clicksToEdit:1,
        renderTo: 'news-grid',
        iconCls: 'icon-grid',
        frame: true,        
        height: eval(Ext.getBody().getViewSize().height - 280),
        store: store,
        plugins: [filters],
        colModel: createColModel(),
        listeners: {
            render: {
                fn: function(){
                    store.load({
                        params: {
                            start: 0,
                            limit: total_row
                        }
                    });
                }
            }
        },
        bbar: pagingBar,
        viewConfig: {
            forceFit: true
        }
    });

    function onAdd(btn, ev) {
       window.location.href = base_url+'admin/onlinesupport/edit';
    }

    function onDelete() {
        Ext.MessageBox.show({
            title:'Delete item',
            msg: 'Do you really want to delete this item ?',
            buttons: Ext.MessageBox.YESNO,
            animEl: 'samplebutton',
            icon: Ext.MessageBox.QUESTION,
            fn:function(buttonId){
                if (buttonId == 'yes'){
                    var recs = newsGrid.getSelectionModel().getSelections() ;
                    if (!recs) {
                        return false;
                    }
                    for(var i in recs)
                    {
                        newsGrid.store.remove(recs[i]);
                    }
                }
            }
        });
    }
});
function formatDate(value){
    return value ? value.dateFormat('d-m-Y') : '';
}
function formatLanguage(value){
    if(value!='')
            return eval('langList.'+value);
    else return '';
}
function status_render(value){
    var src = [{value:1,text:'Available'},{value:0,text:'Unavailable'}];
    var i;
    for(i=0;i<src.length;i++){
        if(value == src[i].value) return src[i].text;
    }
}
